//
//  PayOrderSetupTwo.m
//  HB
//
//  Created by 陶秋安 on 16/4/7.
//  Copyright © 2016年 孙智慧. All rights reserved.

#import "WXApi.h"
#import "PayOrderSetupTwo.h"
#import "SubmitOrderOperation.h"

@implementation PayOrderSetupTwo

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.lblAmount.text=[NSString stringWithFormat:@"￥%@",_amount];
    
    self.lblPoint.text= [NSString stringWithFormat:@"%@积分",_point];
 
    _tablePayWay.delegate=self;
    
    _tablePayWay.dataSource= self;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * const kCellIdentifier = @"paywaytable";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
    
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellIdentifier];
    }
    
    cell.textLabel.text=@"微信支付";
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if(indexPath.row==0)
    {
        [self waitingFor:@"正在支付" andMethod:@selector(myTask) onTarget:self];
    }
}

- (void)myTask
{
    NSString *vipName=@"购买积分";
    
    operation=[[SubmitOrderOperation alloc] initWithDelegate:self];
    
    //开始保存数据
    NSString *url= [FxGlobal getServiceUrl:@"Pay" andFuncName:@"getPrepayId"];
    
    NSString *finalUrl= [NSString stringWithFormat:@"%@?body=%@&out_trade_no=%@&total_fee=%@&spbill_create_ip=127.0.0.1",url,vipName,_orderCode,_amount];
    
    NSDictionary *dictInfo = @{@"url":finalUrl};
    
    [operation setOpInfo:dictInfo];
    
    [operation get:1];
    
}

-(void) opSuccessForGet:(id)data andFlag:(NSInteger)flag
{
    operation=nil;
    
    NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    
    NSDictionary *dataParse  = [dataStr objectFromJSONStringWithParseOptions:JKParseOptionLooseUnicode];
    
    if ([[dataParse objectForKey:@"result"] isEqualToString:@"1"])
    {
        [self pay:dataParse];
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

- (IBAction)btnBackClick:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    return 1;
}

-(void) pay:(NSDictionary *)dict
{
    NSMutableString *stamp  = [dict objectForKey:@"timestamp"];
    //调起微信支付
    PayReq* req             = [[PayReq alloc] init];
    req.partnerId           = [dict objectForKey:@"partnerid"];
    req.prepayId            = [dict objectForKey:@"prepayid"];
    req.nonceStr            = [dict objectForKey:@"noncestr"];
    req.timeStamp           = stamp.intValue;
    req.package             = [dict objectForKey:@"package"];
    req.sign                = [dict objectForKey:@"sign"];
    [WXApi sendReq:req];
}

@end
